Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QHC-833] Improving digital Transpilation #862

Open
wants to merge 30 commits into
base: main
Choose a base branch
from

Conversation

GuillermoAbadLopez
Copy link
Contributor

@GuillermoAbadLopez GuillermoAbadLopez commented Dec 23, 2024

TODOs for this PR:

(each of the next points is pretty big deal, so check them all please 🙌🙃)

  • Move optimize flag, for actual optional optimizations (& Improve optimize word use in methods names)
  • Make Transpilation/execute/compile only for single circuits (unify code standard across qililab)
  • Make Transpilation efficient, by not constructing the Circuit class so many times, between methods
  • Pass a transpilation kwargs instead of so many args in platform/qililab's execute(...)
  • Improve documentation on transpilation, simplifying it in execute()'s, and moving it to a new section.
  • Cover/change test for all of this.

You will see two empty methods, in this next PR I will fill them:

bunch_drag_gates(...), delete_gates_with_no_amplitude(...)



The documentation after this PR looks like:

For the platform.execute():

Screenshot 2024-12-30 at 12 46 37 Screenshot 2024-12-30 at 12 47 43

For the module about Transpilation:

Screenshot 2024-12-30 at 13 39 06 Screenshot 2024-12-30 at 13 39 19

For the Transpiler.transpile_circuit():

Screenshot 2024-12-30 at 13 36 45 Screenshot 2024-12-30 at 13 37 02

@GuillermoAbadLopez GuillermoAbadLopez self-assigned this Dec 23, 2024
Copy link

🔗Pullpo.io Slack channel

Copy link

pullpo-for-slack bot commented Dec 23, 2024

AI Analysis

Review:

The changes in circuit_transpiler.py to add phases from RZs and CZs gates to Drag gates, optimize transpilation by combining consecutive Drags, and adding explanatory comments seem well-structured and clear. It's great to see the addition of optimization methods for the transpilation process. In circuit_optimizer.py, the addition of the method to add phases to Drag gates and optimize transpilation by combining consecutive Drags aligns well with the changes made in circuit_transpiler.py. It's good to maintain consistency across the codebase.

Detailed file changes

(dropdown):

In circuit_transpiler.py:

  • Added a method to add phases from RZs and CZs gates to Drag gates in the circuit.
  • Modified the transpile_circuits method to use the new method to add phases to Drags.
  • Added comments explaining the process of adding phases from RZs and CZs gates to Drags.
  • Added a method to optimize transpilation by bunching consecutive Drag gates together into a single one.

In circuit_optimizer.py:

  • Added a method to add phases from RZs and CZs gates to Drag gates in the circuit.
  • Modified the cancel_pairs_of_hermitian_gates method signature to return a Circuit.
  • Added a method to optimize transpilation by bunching consecutive Drag gates together into a single one.

@GuillermoAbadLopez GuillermoAbadLopez changed the title Improving optimization word use in the transpilation [QHC-833] Improving optimization word use in the transpilation Dec 28, 2024
Copy link

linear bot commented Dec 28, 2024

@GuillermoAbadLopez GuillermoAbadLopez changed the title [QHC-833] Improving optimization word use in the transpilation [QHC-833] Improving Transpilation Dec 28, 2024
@GuillermoAbadLopez GuillermoAbadLopez changed the title [QHC-833] Improving Transpilation [QHC-833] Improving/Feedback Transpilation Dec 28, 2024
@GuillermoAbadLopez GuillermoAbadLopez changed the title [QHC-833] Improving/Feedback Transpilation [QHC-833] Improving/Feedback in Transpilation Dec 28, 2024
@GuillermoAbadLopez GuillermoAbadLopez changed the title [QHC-833] Improving/Feedback in Transpilation [QHC-833] Improving/Feedback for Transpilation Dec 28, 2024
Copy link

codecov bot commented Dec 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.30%. Comparing base (7226edf) to head (2584953).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #862      +/-   ##
==========================================
+ Coverage   96.77%   97.30%   +0.53%     
==========================================
  Files         221      231      +10     
  Lines        7756     8283     +527     
==========================================
+ Hits         7506     8060     +554     
+ Misses        250      223      -27     
Flag Coverage Δ
unittests 97.30% <100.00%> (+0.53%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@GuillermoAbadLopez GuillermoAbadLopez changed the title [QHC-833] Improving/Feedback for Transpilation [QHC-833] Improving digital Transpilation Dec 30, 2024
Copy link
Collaborator

@fedonman fedonman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job! Left some comments.

router: Router | type[Router] | tuple[type[Router], dict] | None = None,
routing_iterations: int = 10,
optimize: bool = True,
program: Circuit | list[Circuit], runcard: str | dict, nshots: int = 1, transpile_config: dict = {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like replacing the parameters with a generic dictionary. Now, it is not clear what the dictionary should contain, what are the default values, there is no autocomplete etc.

In general, having all options as separate parameters is preferred.

If we really do need to use a single parameter, we should use a NamedDict or a new class.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, default parameter values should not be mutable: https://docs.astral.sh/ruff/rules/mutable-argument-default/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, default parameter values should not be mutable: https://docs.astral.sh/ruff/rules/mutable-argument-default/

Damn, true... I guess I'll have to set it back to None then... started like that, but that made the code much uglier, with more checks, that complicated it... 😅. But definitely can go back to it, makes sense 👌

Copy link
Contributor Author

@GuillermoAbadLopez GuillermoAbadLopez Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like replacing the parameters with a generic dictionary. Now, it is not clear what the dictionary should contain, what are the default values, there is no autocomplete etc.

In general, having all options as separate parameters is preferred.

If we really do need to use a single parameter, we should use a NamedDict or a new class.

Yeah I didn't like it either, but it was so many parameters, which might keep growing..., all of them related to the same thing, that in the end discussing it with @visagim we thought, it didn't make sense to keep the params separated, mixing with the other compile/execute parameters, when they clearly are more "modular" 😓. (That's also why I only did it for compile/execute's, but not for the transpile methods down the line)

I also tried first to do a default class, but what I didn;t like about it, is the user having to import and initialize a random class, from a file in qililab, dunno how intuitive that would be 😅. But its true that at least you would then have initialization.

Dunno I'm not 100% convinced what is better, I can try to generate the three examples:

  • Keep params separated
  • Ganeric kwargs
  • Specific qililab dataclass

and ask again in a Code Discussion/daily, where everybody can see it and decide 👍 :

src/qililab/platform/platform.py Outdated Show resolved Hide resolved
@@ -70,6 +70,7 @@ def __init__(
# 3) Layout stage, where the initial_layout will be created.

def route(self, circuit: Circuit, iterations: int = 10) -> tuple[Circuit, dict[str, int]]:
# Docstring related to the public method: :meth:`.CircuitTranspiler.route_circuit()`. Change it there too.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment needed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good point... I actually have a problem here, which I wanted to ask about:

If you see, Inside the Transpiler we have basically methods duplicated from the Optimizer..., since we removed the handling of multiple circuits at the same time.. One option would be to remove such duplication, but at the same time that duplications serves as a kind of interface to add more functionality, and shows the user which functions can be called from the transpiler directly, so its not super bad...

Of course this makes that the docstrings are duplicated, and that is more problematic, since we wouldn't; want our devs, only updating the Optimizer docstrings, but not the Transpiler's which are more public. So the options we have are:

  • Delete the duplication in some way (I think this will complicate how we show public method to the user...)
  • Add this silly comment for devs to notice, that they need to change the public documentation always
  • Maybe just delete the docstring from the not public methods? So there is no mistake by the devs? And they know to go and search elsewhere, which docstring to change 🤔...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll think more about it the next days, and ask the group with a clearer head...

But such problem make me notice there is a structural code problem from base with having this duplication, which maybe should be addressed more deeply... But 😬 ...

Copy link
Contributor Author

@GuillermoAbadLopez GuillermoAbadLopez Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fedonman, I ended up deciding to delete a bunch of duplicated docstrings, so we don't have confusion on where to update them!

And deleted the comments you pointed out, instead now I added inside the duplicated docstring itself, check if you agree with the implementation 🙌

src/qililab/digital/circuit_to_pulses.py Outdated Show resolved Hide resolved
src/qililab/digital/circuit_transpiler.py Outdated Show resolved Hide resolved
docs/fundamentals/transpilation.rst Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants